package pos.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import Clases.domain.Causa;

public class JDBCCausaDAO implements ICausaDAO {

	public List selectAllCausasZona(Connection conn, Integer zonaOID) {

		PreparedStatement stmt = null;
		List searchResults = new LinkedList();
		ResultSet result = null;
		Causa c;
		try {
			String sql = "SELECT * FROM Causa C WHERE C.zonaOID ='" + zonaOID
					+ "'";
			stmt = conn.prepareStatement(sql);
			result = stmt.executeQuery();

			while (result.next()) {
				c = new Causa();
				c.setBarrio(result.getString("nomBarrio"));
				c.setZona(result.getString("nomZona"));
				c.setCausaDescripcion(result.getString("descripcion"));
				c.setCausaNumApoyos(result.getInt("numApoyos"));
				searchResults.add(c);
			}

		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (result != null)
					result.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}

		}
		return searchResults;
	}

	public Causa selectCausasManifestacion(Connection conn, Integer manifestacionOID) {

		PreparedStatement stmt = null;
		ResultSet result = null;
		Causa c = null;
		try {
			String sql = "SELECT * FROM Causa C WHERE C.manifestacionOID ='"+ manifestacionOID + "'";
			stmt = conn.prepareStatement(sql);
			result = stmt.executeQuery();

			if (result.next()) {
				c = new Causa();
				c.setBarrio(result.getString("nomBarrio"));
				c.setZona(result.getString("nomZona"));
				c.setCausaDescripcion(result.getString("descripcion"));
				c.setCausaNumApoyos(result.getInt("numApoyos"));

			}

		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (result != null)
					result.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}

		}
		return c;
	}

}
